<template>
  <n-space vertical :size="12">
    <n-data-table
      id="table"
      :bordered="false"
      :single-line="false"
      :columns="columns"
      :data="data"
      :pagination="pagination"
    />
  </n-space>
</template>

<script>
import { h, defineComponent, toRefs, toRef, onMounted } from 'vue'
import { NTag, NButton, useMessage } from 'naive-ui'
import { ref } from 'vue'
import { toRaw } from '@vue/reactivity'
import { searchHistory } from '@/api/post'
import { createLocalStorage } from '@/utils/cache'
const createColumns = ({ sendMail }) => {
  return [
    {
      title: '任务状态',
      key: 'status1',
      // render(row) {
      //   const tags = row.tags.map((tagKey) => {
      //     return h(
      //       NTag,
      //       {
      //         style: {
      //           marginRight: '6px',
      //         },
      //         type: 'info',
      //       },
      //       {
      //         default: () => tagKey,
      //       }
      //     )
      //   })
      //   return tags
      // },
    },
    {
      title: '任务名称',
      key: 'task_name',
    },
    {
      title: '任务类型',
      key: 'type1',
    },
    {
      title: '可提交(次)',
      key: 'times',
    },
    {
      title: '金额',
      key: 'account',
    },
    {
      title: '日期',
      key: 'create_at',
    },

    {
      title: '操作',
      key: 'actions',
      render(row) {
        return h(
          NButton,
          {
            size: 'small',
            onClick: () => sendMail(row),
          },
          { default: () => '打开' }
        )
      },
    },
  ]
}

const createData = () => [
  {
    key: 0,
    task_name: '垃圾回收',
    task_type: '分类',
    times: '1',
    account: 1,
    date: '2022-01-01',
    tags: ['nice'],
  },
  {
    key: 1,
    task_name: '商品识别',
    task_type: '打框',
    times: '2',
    account: 2,
    date: '2022-01-02',
    tags: ['wow'],
  },
  {
    key: 2,
    task_name: '人脸识别',
    task_type: '打点',
    times: '2',
    account: 3,
    date: '2022-01-03',
    tags: ['cool'],
  },
]

export default defineComponent({
  // props: ['tasks'],
  // setup(props) {
  setup() {
    onMounted(() => {
      const ls = createLocalStorage({ prefixKey: 'login_' })
      const user = ls.get('loginInfo')
      // console.log(user.userid)
      search(user.userid)
    })
    const hhtasks = ref([])
    async function search(id) {
      const res = await searchHistory({
        userid: id,
      })
      hhtasks.value = res
      console.log('hhtasks')
      console.log(hhtasks.value)
    }

    const types = ['所有', '分类', '打点', '打框']
    const statuss = ['所有', '已完成', '稽核中', '校验中']
    // const { mytask } = toRefs(props)
    // const mytask = toRef(props, 'tasks')
    // console.log('mytask')
    // console.log(mytask.value)
    let tasks = toRaw(hhtasks)
    // let tasks = toRaw(props.tasks)
    // console.log('进入了表格list')
    // console.log('props.tasks=')
    console.log('tasks')
    console.log(tasks)
    for (let i in tasks) {
      // console.log(i)
      console.log(tasks[i])
      tasks[i].type1 = types[tasks[i].type] //中文type
      tasks[i].status1 = statuss[tasks[i].status] //中文status
    }
    // console.log(createData())
    const message = useMessage()
    return {
      data: tasks,
      columns: createColumns({
        sendMail(rowData) {
          console.log(1111111111111111)
          message.info('send mail to ' + rowData.task_id)
          if (rowData.type == 2) {
            window.open('http://192.168.175.1:8080/mark2/?task_id=' + rowData.task_id)
          } else if (rowData.type == 1) {
            window.open('http://192.168.175.1:8080/mark3/?task_id=' + rowData.task_id)
          } else if (rowData.type == 3) {
            window.open('http://192.168.175.1:8080/mark4/?task_id=' + rowData.task_id)
          }
        },
      }),
      pagination: {
        pageSize: 10,
      },
    }
  },
})
</script>

<style lang="scss" scoped>
#table {
  width: 90%;
}
</style>
